<?php

require_once 'database/DBConnection.php';

class UserHandler {
	
	private $m_dbConnection = NULL;
	private $m_userTableName = "user";
	
	
	public function __construct (DBConnection $a_connection) {
		$this->m_dbConnection = $a_connection;
	}
	
	// -----------------------------------INSERT user------------------------------
	public function InsertUser ($username, $email, $password) {

	$sql = "INSERT INTO $this->m_userTableName (m_username, m_email, m_password) VALUES(?,?,?)";
	
	$stmt = $this->m_dbConnection->Prepare($sql);
	
	$stmt->bind_param("sss", $username, $email, $password);
	
  	if ($stmt === FALSE) {
    	return false;
    }
	
	if ($stmt -> execute()) {
		$stmt -> close();
		return TRUE;
	}else{
		$stmt->close();
		return FALSE;
		}

	}
	
	/**
	 * CREATE TABLE IF NOT EXISTS `user` (
  	`pk` int(11) NOT NULL AUTO_INCREMENT,
  	`m_username` varchar(100) NOT NULL,
  	`m_password` varchar(25) NOT NULL,
  	PRIMARY KEY (`pk`)
	) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
	 */
	
	// -------------------------DELETE -------------------------
	public function DeleteUser($username) {
		$sql = "DELETE FROM $this->m_userTableName WHERE m_username = ?";
	
		$stmt = $this->m_dbConnection->Prepare($sql);
		
		$stmt->bind_param("s", $username);
		
		$stmt -> execute();
	
		$stmt -> close(); 

	return TRUE;
	}
	
	
	// --------------------GET-------------------------------//Används EJ
	public function GetAllUsers() {
		$ret = array();
		
		$sql = "SELECT * FROM this->m_userTableName";
		
		$stmt = $this->m_dbConnection->Prepare($sql);
		
		$stmt->execute();
		
		$result = $stmt->get_result();
		
		while ($object = $result->fetch_object("model\User")) {
			$ret[$object->pk] = $object;
		}
		
		$stmt->close();
		
		return $ret;
	}
	
	
	// By MAIL (vet egentligen allt om användaren, och inte bara e-post----------------------------------------------
	public function GetUserMail($id) {
		$ret = NULL;
		
		$sql ="SELECT * FROM $this->m_userTableName WHERE pk = ?";
		
        $stmt = $this->m_dbConnection->Prepare($sql);
		
		$stmt->bind_param("i", $id);
		
		$stmt->execute();
		
		$ret = $this->get($stmt);
		
		$stmt->close();
                
        return $ret[0]->m_email; 
	}
	
	
	// ----BY ID--ANVÄNDS EJ!!!-----------------------------
	public function GetUserById($a_id) {
		$ret = NULL;
		
		$sql ="SELECT * FROM $this->m_userTableName WHERE pk = ?";
		
		$stmt = $this->m_dbConnection->Prepare($sql);
		
		$stmt->bind_param("i", $a_id);
		
		$stmt->execute();
		
		//Get_result funkar ej på webbhotell
		$result = $stmt->get_result();
		
		while ($object = $result->fetch_object("Model\User")) {
			$ret = $object;
		}
		
		$stmt->close();
		
		return $ret;
		
	}
	//------------------------------------------------------------------
	//Istället för get_result
	public function get($stmt){
		$ret = array();
		//Alla kolumner i tabellen
		$stmt->bind_result($pk, $m_username, $m_email, $m_password);
		
		while ($stmt->fetch()) {
			$user = new Product();
			$user->pk = $pk;
			$user->m_username =$m_username;
			$user->m_email=$m_email;
			$user->m_password = $m_password;
			
        	$ret[] = $user;
    	}
		
		return $ret;
	}
	
}
